import { saveArticle, recArticles, curArticle, upArticle } from '../services/articles';
import { message } from 'antd';
import { routerRedux } from "dva/router";

export default {
    namespace: 'articles',
    state: {
        recent: [],
        entities: {},
    },
    effects: {
        *addArticle({payload}, {call, put}) {
            yield call(saveArticle, payload)
            message.success('Save success')
            yield put(routerRedux.push('/articles/recent'))
        },
        *getRecent(_, {call, put}) {
            const resp = yield call(recArticles)
            yield put({
                type: 'setRecent',
                payload: resp
            })
        },
        *getArticleById({articleId}, {call, put}) {
            const resp = yield call(curArticle, articleId)
            yield put({
                type: 'setCurrent',
                payload: resp
            })
        },
        *updateArticle({ payload }, {call, put}) {
            const resp = yield call(recArticles)
            yield put({
                type: 'changeArticle',
                payload: {
                    resp,
                    payload
                }
            })
            message.success('Save success')
        },
        *deleteArticle({article_Id}, {call, put}) {
            const resp = yield call(recArticles)
            yield put({
                type: 'delArticle',
                payload: {
                    resp,
                    article_Id
                }
            })
        }
    },
    reducers: {
        setRecent(state, action) {
            //reduce(*function, initValue)
            //the *function: ( finalValueS, inputValueS ) {
            //    CfinalValue[idFlag] = CinputValue
            //    return CfinalValue/S
            //}
            const newEntities = action.payload.reduce((acc, art) => {
                acc[art.id] = art
                return acc
            }, {})
            return {
                ...state,
                recent: action.payload,
                entities: {
                    ...state.entities,
                    ...newEntities
                }
            }
        },
        setCurrent(state, action) {
            return {
                ...state,
                entities: {
                    ...state.entities,
                    [action.payload.id]: action.payload
                }
            }
        },
        changeArticle(state, action) {
            const newRecent = action.payload.resp
            newRecent[action.payload.payload.id] = action.payload.payload
            return {
                ...state,
                recent: newRecent,
                entities: {
                    ...state.entities,
                    [action.payload.payload.id]: action.payload.payload 
                }
            }
        },
        delArticle(state, action) {
            const newRecent = action.payload.resp
            newRecent.splice(action.payload.article_Id, 1)
            const newEntities = action.payload.resp.reduce((acc, art) => {
                acc[art.id] = art
                return acc
            }, {})
            delete newEntities[action.payload.article_Id]
            return {
                ...state,
                recent: newRecent,
                entities: {
                    ...state.entities,
                    ...newEntities
                }
            }
        }
    }
}